package com.cd.university.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cd.university.common.RestResult;
import com.cd.university.db.pojo.Score;
import com.cd.university.service.AssessmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈栋
 * @create 2021/11/11 19:26
 */
@RestController
@RequestMapping("/assessment")
@Api("科研考核")
public class AssessmentController {

    @Autowired
    private AssessmentService assessmentService;

    @GetMapping("/getAllProgram")
    @ApiOperation("获得所有的考核方案")
    public RestResult assessmentGetAll(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                       @RequestParam(required = false,defaultValue = "6") Integer pageSize) {
        return assessmentService.getAssessmentProgram(pageNum, pageSize);
    }

    @GetMapping("/getAllProject")
    @ApiOperation("获得所有的考核的项目")
    public RestResult getAllAssessmentProject(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                              @RequestParam(required = false,defaultValue = "6") Integer pageSize) {
        return assessmentService.getAllAssessmentProject(pageNum, pageSize);
    }

    @GetMapping("/searchByName")
    @ApiOperation("根据项目的名称搜索考核项目")
    public RestResult searchAssessmentProjectByProjectName(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                           @RequestParam(required = false,defaultValue = "6") Integer pageSize,
                                                           @RequestParam("projectName") String projectName) {
        return assessmentService.searchAssessmentProjectByProjectName(pageNum, pageSize, projectName);
    }

    @GetMapping("/searchByDeId")
    @ApiOperation("根据考核项目的单位筛选")
    public RestResult searchAssessmentProjectByDepartmentId(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                            @RequestParam(required = false,defaultValue = "6") Integer pageSize,
                                                            @RequestParam("departmentId") Integer departmentId) {
        return assessmentService.searchAssessmentProjectByDepartmentId(pageNum, pageSize, departmentId);
    }

    @PostMapping("/updateScores")
    @ApiOperation("修改分数")
    public RestResult updateScores(@RequestBody JSONObject jsonObject) {
        JSONArray jsonArray = jsonObject.getJSONArray("scoreList");
        List<Score> scoreList = jsonArray.toJavaList(Score.class);

        return assessmentService.updateScores(scoreList);
    }
}
